package work.parabola.keyboardswitch.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.parabola.keyboardswitch.common.ResponseCode;
import work.parabola.keyboardswitch.common.RoleEnum;
import work.parabola.keyboardswitch.common.ServerResponse;
import work.parabola.keyboardswitch.common.UserConst;
import work.parabola.keyboardswitch.pojo.UserSession;
import work.parabola.keyboardswitch.pojo.query.storeswitch.InputQuery;
import work.parabola.keyboardswitch.pojo.query.storeswitch.ListQuery;
import work.parabola.keyboardswitch.pojo.query.storeswitch.NoteQuery;
import work.parabola.keyboardswitch.pojo.query.storeswitch.OutputQuery;
import work.parabola.keyboardswitch.service.IStoreSwitchService;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/store-switch/")
public class StoreSwitchController {

    @Autowired
    private IStoreSwitchService iStoreSwitchService;

    @PostMapping("getAdminSwitchStoreList.do")
    public ServerResponse getAdminSwitchStoreList(HttpSession session, @RequestBody ListQuery listQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iStoreSwitchService.getAdminSwitchStoreList(listQuery, userSession);
    }

    @PostMapping("refreshAdminSwitchStore.do")
    public ServerResponse refreshAdminSwitchStore(HttpSession session) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iStoreSwitchService.refreshAdminSwitchStore(userSession);
    }

    @PostMapping("upwardAdminSwitchStore.do/{id}/{count}")
    public ServerResponse upwardAdminSwitchStore(HttpSession session, @PathVariable Integer id, @PathVariable Integer count) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iStoreSwitchService.upwardAdminSwitchStore(id, userSession, count);
    }

    @PostMapping("downwardAdminSwitchStore.do/{id}/{count}")
    public ServerResponse downwardAdminSwitchStore(HttpSession session, @PathVariable Integer id, @PathVariable Integer count) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iStoreSwitchService.downwardAdminSwitchStore(id, userSession, count);
    }

    @PostMapping("updateAdminSwitchStoreInfo.do")
    public ServerResponse updateAdminSwitchStoreInfo(HttpSession session, @RequestBody NoteQuery noteQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iStoreSwitchService.updateAdminSwitchStoreNote(noteQuery, userSession);
    }

    @PostMapping("inputAdminSwitchStore.do")
    public ServerResponse inputAdminSwitchStore(HttpSession session, @RequestBody InputQuery inputQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iStoreSwitchService.inputAdminSwitchStore(inputQuery, userSession);
    }

    @PostMapping("outputAdminSwitchStore.do")
    public ServerResponse outputAdminSwitchStore(HttpSession session, @RequestBody OutputQuery outputQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iStoreSwitchService.outputAdminSwitchStore(outputQuery, userSession);
    }

    @GetMapping("getAdminSwitchStoreDetail.do/{id}")
    public ServerResponse getAdminSwitchStoreDetail(HttpSession session, @PathVariable Integer id) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iStoreSwitchService.getAdminSwitchStoreDetail(id, userSession);
    }

    @DeleteMapping("deleteAdminSwitchStoreItem.do/{id}")
    public ServerResponse deleteAdminSwitchStoreItem(HttpSession session, @PathVariable Integer id) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iStoreSwitchService.deleteAdminSwitchStoreItem(id, userSession);
    }
}
